package com.mobbanana.gameHelper;

import android.util.Log;

import java.io.File;


/**
 * @Description
 * @Author imchen
 * @CreateTime 2019/4/16 10:43
 */
public class LogUtil {

    private final static String TAG = "iLogger";
    
    enum Level {
        INFO, VERBOSE, WARN, DEBUG, ASSERT, ERROR
    }

    private static boolean isDebug = false;

    static {
        File file = new File("data/local/tmp/i.debug");
        if (file.exists() && file.isFile()) {
            isDebug = true;
        }
    }


    public static final String tag = "TLogger";

    public static void d(String msg) {
        d(tag, msg);
    }

    public static void w(String msg) {
        w(msg);
    }

    public static void i(String msg) {
        i(tag, msg);
    }

    public static void e(String e) {
        e(tag, e);
    }

    public static void d(String tag, String msg) {
        if (isDebug) {
            String[] info = getAutoJumpLogInfo(Level.DEBUG);
            Log.d(tag, info[0] + ":" + info[1] + info[2] + "-> " + msg);
        }
    }

    public static void w(String tag, String msg) {
        if (isDebug) {
            String[] info = getAutoJumpLogInfo(Level.WARN);
            Log.w(tag, info[0] + ":" + info[1] + info[2] + "-> " + msg);
        }
    }

    public static void i(String tag, String msg) {
        if (isDebug) {
            String[] info = getAutoJumpLogInfo(Level.INFO);
            Log.i(tag, info[0] + ":" + info[1] + info[2] + "-> " + msg);
        }
    }

    public static void e(String tag, Throwable e) {
        if (isDebug) {
            String[] info = getAutoJumpLogInfo(Level.ERROR);
            Log.e(tag, info[0] + ":" + info[1] + info[2] + "-> " + e.getMessage());
        }
    }

    public static void e(String tag, String e) {
        if (isDebug) {
            String[] info = getAutoJumpLogInfo(Level.ERROR);
            Log.e(tag, info[0] + ":" + info[1] + info[2] + "-> " + e);
        }
    }

    public static void e(Throwable t) {
        e(tag, t);

    }

    private static boolean debuggable = true;

    /**
     * Debug 日志开关,Release版本最好关闭
     *
     * @param isOpen Boolean
     */
    public static void setDebuggable(boolean isOpen) {
        debuggable = isOpen;
    }


    public static void dev(String msg) {
        if (debuggable) {
            Log.d(TAG, msg);
        }
    }

    public static void dev(Exception e) {
        if (debuggable) {
            Log.d(TAG, e.getMessage());
        }
    }


    /**
     * 获取打印信息所在方法名，行号等信息
     * 输出规则 类名:方法名@行号-> 参数
     *
     * @return String[]
     */
    private static String[] getAutoJumpLogInfo(Level level) {
        String[] info = new String[]{"", "", ""};
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        if (elements.length < 5) {
            Log.e("MyLogger", "Stack is too shallow!!!");
            return info;
        } else {
            info[0] = elements[5].getClassName().substring(
                    elements[5].getClassName().lastIndexOf(".") + 1);
            info[1] = elements[5].getMethodName() + "()";
            if (level == Level.ERROR) {
                info[2] = " at [" + elements[5].getClassName() + ".java@"
                        + elements[5].getLineNumber() + "]";
            } else {
                info[2] = "@" + elements[5].getLineNumber() + "";
            }

            return info;
        }
    }

}
